package sort;

import java.util.Arrays;


/**冒泡排序*/
public class BubbleSort {
    public static void main(String[] args) {
        int [] arr = {486,198,76,4649,649,61,8,1,98,9864,};
        System.out.println(Arrays.toString(arr));
        bubbleSort(arr);
        System.out.println(Arrays.toString(arr));
    }
    private static void bubbleSort( int [] arr){
        boolean flag;
        int temp;
        //王海涛老师讲的
        for (int j = arr.length-1; j >0 ; j--) {
            flag = false;
            for (int i = 0; i < j; i++) {
                if(arr[i]>arr[i+1]){
                    temp = arr[i];
                    arr[i]=arr[i+1];
                    arr[i+1]=temp;
                    flag = true;
                }
            }
            if(!flag)break;
            System.out.println(Arrays.toString(arr));
        }

        //自己写的冒泡排序
        /*
        for (int j = 0; j<arr.length-1; j++) {
            for (int i = 0; i <arr.length-1-j ; i++) {
                if(arr[i]<arr[i+1]){
                    int temp =arr[i];
                    arr[i]=arr[i+1];
                    arr[i+1]=temp;
                    System.out.println(Arrays.toString(arr));
                }
            }
        }
         */

    }
}
